package com.content.scaffold.common.client.base;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.notarycloud.client.NotaryCloudClient;
import com.notarycloud.client.NotaryCloudResponse;
import com.notarycloud.client.NotaryCloudSimpleResponse;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @author xiaojl
 *  外呼请求基础类
 */

@Slf4j
public class BaseCloudClientService {

    private final NotaryCloudClient notaryCloudClient;



    public BaseCloudClientService(NotaryCloudClient notaryCloudClient) {
        this.notaryCloudClient = notaryCloudClient;
    }

    /**
     * 内部不直接抛错，由外部自行判断是否抛异常
     */
    public <RESPONSE, REQUEST> ExternalBaseResponse<RESPONSE> doExchangeRequest(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();

        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).exchange();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.response(responseClass, exchange);

    }



    public <RESPONSE, REQUEST> ExternalBaseResponse<List<RESPONSE>>doExchangeRequestReturnList(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).exchange();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.responseList(responseClass, exchange);

    }




    /**
     * 内部不直接抛错，由外部自行判断是否抛异常
     *
     */
    public <RESPONSE, REQUEST> ExternalBaseResponse<RESPONSE> doSubmitRequest(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();

        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).submit();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.response(responseClass, exchange);

    }
    public <RESPONSE, REQUEST> ExternalBaseResponse<List<RESPONSE>> doSubmitRequestReturnList(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).submit();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.responseList(responseClass, exchange);

    }



    /**
     * 内部不直接抛错，由外部自行判断是否抛异常
     *
     */
    public <RESPONSE, REQUEST> ExternalBaseResponse<RESPONSE> doQueryRequest(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();

        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).query();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.response(responseClass, exchange);

    }

    public <RESPONSE, REQUEST> ExternalBaseResponse<List<RESPONSE>> doQueryRequestReturnList(String taskName, REQUEST request, Class<RESPONSE> responseClass, String requestUrl) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest().setRequestId(requestId).setData(request).setPath(requestUrl).query();
        setLogInfo(taskName, request, requestUrl, requestId, exchange);
        return ExternalBaseResponse.responseList(responseClass, exchange);

    }

    private String getRequestId() {
        return IdUtil.simpleUUID();
    }


    private <REQUEST> void setLogInfo(String taskName, REQUEST request, String requestUrl, String requestId, NotaryCloudSimpleResponse exchange) {
        log.info("外呼： {} ; url : {} , requestId ： {} ; request json ： {}   ;  response json   {}  ;", taskName, requestUrl, requestId, JSONUtil.toJsonStr(request), JSONUtil.toJsonStr(exchange));
    }



    public NotaryCloudResponse submit(Object data, String url) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse submit = notaryCloudClient.newRequest(data).setPath(url).setRequestId(requestId).submit();
        setLogInfo("统一调用submit",data,url,requestId,submit);
        return submit;
    }

    public NotaryCloudResponse query(Object data, String url) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse query = notaryCloudClient.newRequest(data).setPath(url).setRequestId(requestId).query();
        setLogInfo("统一调用query",data,url,requestId,query);
        return query;
    }

    public NotaryCloudResponse exchange(Object data, String url) {
        String requestId = getRequestId();
        NotaryCloudSimpleResponse exchange = notaryCloudClient.newRequest(data).setPath(url).setRequestId(requestId).exchange();
        setLogInfo("统一调用exchange",data,url,requestId,exchange);
        return exchange;
    }
}
